package com.example.ui.share;

import java.util.ArrayList;
import java.util.List;

import com.example.huashangapp.R;
import com.example.net.AnalyzingJson;
import com.example.net.NetRequest;
import com.example.ui.learn.InviteActivity;
import com.example.ui.learn.util.WheelMain;
import com.example.util.WheelView;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.DatePicker.OnDateChangedListener;

public class SeeActivity extends Activity {
	private EditText ia_etcompany, ia_etaddress, ia_etname, ia_etmobilephone,
			ia_etemail, ia_etphone;
	private String company, address, name, mobilephone, email, phone, place,
			reason;
	// top
	private Button btnleft, btnright;
	private TextView tvtitle;
	private RelativeLayout sa_selectdate;
	private EditText sa_place, sa_reason;
	
	private LinearLayout sa_ll;

	private String time = "";
	private Button sa_ok;

	
	private TextView sa_tvdays;
	private List<String> list= new ArrayList<String>();
	private String object;
	private String data, result;
	private TextView tvobject;
	String tid,tname;

	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_s);
		Intent i=getIntent();
		tid=i.getStringExtra("tid");
		tname=i.getStringExtra("tName");
		init();
	}

	private void init() {
		btnleft = (Button) findViewById(R.id.layout_title_left_btn);
		btnright = (Button) findViewById(R.id.layout_title_right_btn);
		tvtitle = (TextView) findViewById(R.id.layout_title_middle_text);
		btnleft.setText("返回");
		tvtitle.setText("拜访约见");
		btnright.setText("提交");
        object=tname;
		tvobject = (TextView) findViewById(R.id.as_tvobject);
		tvobject.setText(object);
		sa_place = (EditText) findViewById(R.id.ai_etplace);
		sa_reason = (EditText) findViewById(R.id.ai_etreason);
		//
		ia_etcompany = (EditText) findViewById(R.id.ai_etcompany);
		ia_etaddress = (EditText) findViewById(R.id.ai_etaddress);
		ia_etname = (EditText) findViewById(R.id.ai_etname);
		ia_etmobilephone = (EditText) findViewById(R.id.ai_etmobile);
		ia_etemail = (EditText) findViewById(R.id.ai_etemail);
		ia_etphone = (EditText) findViewById(R.id.ai_etphone);

		sa_selectdate = (RelativeLayout) findViewById(R.id.asee_rldate);
		sa_ll = (LinearLayout) findViewById(R.id.asee_ll);
		

	
		
		sa_tvdays = (TextView) findViewById(R.id.as_tvdays);
		
	
		sa_selectdate.setOnClickListener(sa_ocl);
		sa_ll.setOnClickListener(sa_ocl);
		btnright.setOnClickListener(sa_ocl);
		btnleft.setOnClickListener(sa_ocl);
	}
	AlertDialog infoad;
	AlertDialog.Builder bulider;
    
    DatePicker dp;
    Button okfordp;
	private void initDatePicker(){
		View v=View.inflate(SeeActivity.this, R.layout.selectdate, null);
		bulider = new AlertDialog.Builder(SeeActivity.this);
		bulider.setView(v).create();
		dp=(DatePicker) v.findViewById(R.id.dp);
		okfordp=(Button)v.findViewById(R.id.okfordp);
		okfordp.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				time=dp.getYear()+"-"+dp.getMonth()+"-"+dp.getDayOfMonth();
				sa_tvdays.setText(time);
				infoad.dismiss();
			}
		});
		  Time t = new Time("GMT+8");    
	        t.setToNow();   	      		
		dp.init(t.year, t.month, t.monthDay, new OnDateChangedListener() {
			
			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
			}
		});
	}
	Runnable sumbit_run = new Runnable() {

		@Override
		public void run() {
            Log.i("hs invite" , list.toString());
			data = NetRequest.postRequestVisit(list);
			result = AnalyzingJson.return_iaresult(data);
			h.sendEmptyMessage(1);
		}
	};
	private Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {

			case 1:
				if (result.equals("true")) {
					Toast.makeText(SeeActivity.this, "提交成功", Toast.LENGTH_LONG)
							.show();
					finish();
				} else {
					Toast.makeText(SeeActivity.this, "提交失败，请重试",
							Toast.LENGTH_LONG).show();
				}

				break;
			default:
				break;
			}

		};

	};

	private OnClickListener sa_ocl = new OnClickListener() {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.asee_rldate:
				initDatePicker();
			    infoad=bulider.show();
				break;
			case R.id.layout_title_left_btn:
				finish();
				break;
			case R.id.layout_title_right_btn:
				reason = sa_reason.getText().toString();
				place = sa_place.getText().toString();
				company = ia_etcompany.getText().toString();
				address = ia_etaddress.getText().toString();
				name = ia_etname.getText().toString();
				mobilephone = ia_etmobilephone.getText().toString();
				email = ia_etemail.getText().toString();
				phone = ia_etphone.getText().toString();
				list.add(tid);
				list.add(object);
				list.add(time);
				list.add(place);
				list.add(reason);
				list.add(company);
				list.add(address);
				list.add(name);
				list.add(mobilephone);
				list.add(email);
				list.add(phone);
				Boolean isNull = null;
				for (int i = 0; i < list.size(); i++) {
					String s = list.get(i);
					if (s.equals("")) {
						Toast.makeText(SeeActivity.this, "请完整填写资料",
								Toast.LENGTH_SHORT).show();
						isNull = true;
						break;
					}
					isNull = false;
				}
				if (isNull == false) {
					new Thread(sumbit_run).start();
				}

				break;

			default:
				break;
			}
		}
	};

}
